var bankRequestNo;
$(function () {
    if(orderStr.length >1){
        var order = JSON.parse(orderStr);
        var data = {"order":order};
        var html = template('orderTmp', data);
        document.getElementById('orderContent').innerHTML = html;
    }
    //Date picker
    $("input[name='refundDate']").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    });
    $('#closeReason').attr("disabled",true);
    if(replystr.length >1){
        var reply = JSON.parse(replystr);
        $('#jdremark').val(reply.remark);
        $('#caseState').val(reply.caseState);
        if(reply.caseState == '3'){
            $('#closeReason').val(reply.closeReason);
            $('#closeReason').attr("disabled",false);
        }else{
            $('#closeReason').attr("disabled",true);
        }

        $('#replyId').val(reply.id);
        var fileNames =  reply.jdAttachFile.split(",");
        $.each(fileNames , function(index ,item){
            item = $.trim(item);
            if(item && item.length >0)
            {
                var tmp =  "<span data='"+item+"'>附件：<input type=\"hidden\" name=\"attachFile\" value='"
                    +item +"'> <a href=\"#\">"+item
                    +"</a> <i class=\"fa fa-fw fa-trash-o delete\" onclick=\"deleteFile('"+item+"')\"></i></span>";
                $('#jdFileListDiv').append(tmp);
            }
        });
    }

    $('#saveBtn').click(function(){
        var orders = getAllOrder();
        var reply = getReplyInfo();
        var request = new Object();
        request.id = requestId;
        saveOrSend(orders , reply , request);
    });

    $('#sendBtn').click(function(){
        var orders = getAllOrder();
        var reply = getReplyInfo();
        var request = new Object();
        request.id = requestId;
        request.bankRequestState ='2';
        saveOrSend(orders , reply , request);
    });

    //var consignee = JSON.parse(consigneestr);
   // var payment = JSON.parse(paymentstr);
});

function getReplyInfo()
{
    var obj = new Object();
    obj.id = $("#replyId").val();
    obj.requestId = requestId;
    obj.caseState = $('#caseState').val();
    obj.closeReason =$('#closeReason').val();
    obj.remark = $('#jdremark').val();
    var fileName = "" ,count =0 ;
    $("input[name='attachFile']").each(function(index, item){
        fileName =$(item).val()+","+fileName;
        count++;
    });
    obj.jdAttachFile = fileName;
    obj.jdAttachCount = count;
    return obj;
}



function  getAllOrder()
{
    var orders = [];
    $("input[name='orderId']").each(function(index,item){
        var par = new Object();
        par.id = $(item).val()
        par.requestId = requestId;
        par.bankRequestNo = bankRequestNo;
        orders[index] = par;
    });
    $("input[name='merchantTradeNo']").each(function(index,item){
        orders[index]["merchantTradeNo"] = $(item).val();
    });
    $("input[name='tradeNo']").each(function(index,item){
        orders[index]["tradeNo"] = $(item).val();
    });
    $("input[name='merchantName']").each(function(index,item){
        orders[index]["merchantName"] = $(item).val();
    });

    $("input[name='tradeType']").each(function(index,item){
        orders[index]["tradeType"] = $(item).val();
    });

    $("input[name='tradeStatus']").each(function(index,item){
        orders[index]["tradeStatus"] = $(item).val();
    });

    $("input[name='isRefund']").each(function(index,item){
        orders[index]["isRefund"] = $(item).val();
    });

    $("input[name='refundMoney']").each(function(index,item){
        orders[index]["refundMoney"] = $(item).val();
    });

    $("input[name='refundDate']").each(function(index,item){
        orders[index]["refundDate"] = $(item).val();
    });

    return orders;
}
function subForm(){
    $("#myForm").ajaxSubmit({
        type:"post",  //提交方式
        url:queryPath+"/uploadFile.do",
        dataType:"json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success:function(data){ //提交成功的回调函数
            if(data.success){
                var tmp =  "<span data='"+data.fileName+"'>附件：<input type=\"hidden\" name=\"attachFile\" value='"
                    +data.fileName +"'> <a href=\"javascript:void(0)\" onclick=\"downloadFile('"+data.fileName+"')\">"+data.fileName
                    +"</a> <i class=\"fa fa-fw fa-trash-o delete\" onclick=\"deleteFile('"+data.fileName+"')\"></i></span>";
                $('#jdFileListDiv').append(tmp);
            }
        }
    });
}

function deleteFile(fileName)
{
    $.ajax({
        type: "POST",
        url: queryPath+"/deleteFile.do?fileName="+fileName,
        dataType: "json",
        async: false,
        success: function(msg) {
            if(msg.success)
            {
                $("span[data='"+fileName+"']").remove();
            }
        }
    });

}

function saveOrSend(orders ,reply ,request )
{
    request.jdAttachCount = reply.jdAttachCount;
    $.ajax({
        type: "POST",
        url: queryPath+"/saveOrSendReply.do",
        data:{'orderstr':JSON.stringify(orders) ,'replystr':JSON.stringify(reply),'requeststr':JSON.stringify(request)},
        dataType: "json",
        async: false,
        success: function(msg) {
            if(msg.success)
            {
                alert("OK");
            }
        }
    });
}

function downloadFile(fileName)
{
    window.location.href = queryPath+"/downloadFile.do?fileName="+fileName;
}

function caseChange(obj){
    if($(obj).val() =='3'){
        $('#closeReason').attr("disabled",false);
    }else{
        $('#closeReason').attr("disabled",true);
    }
}